﻿@using System.Net.Http.Json;

<MCard Color="red lighten-2"
       Dark>
    <MCardTitle Class="text-h5 red lighten-3">
        Search for Public APIs
    </MCardTitle>
    <MCardText>
        Explore hundreds of free API's ready for consumption! For more information visit
        <a class="grey--text text--lighten-3"
           href="https://github.com/toddmotto/public-apis"
           target="_blank">
            the GitHub repository
        </a>.
    </MCardText>
    <MCardText>
        <MAutocomplete TItem="Item"
                       TValue="string"
                       TItemValue="string"
                       @bind-Value="_value"
                       Items="@_entries"
                       Loading="_isLoading"
                       OnSearchInputUpdate="UpdateSearchInputAsync"
                       OnSelect="@(u => _model = u.Item)"
                       Color="white"
                       HideNoData
                       HideSelected
                       ItemText="r => r.Description"
                       ItemValue="r => r.Name"
                       Label="Public APIs"
                       Placeholder="Start typing to Search"
                       PrependIcon="mdi-database-search">
        </MAutocomplete>
    </MCardText>
    <MDivider></MDivider>
    <ExpandTransition>
        <ShowTransitionElement Value="@Show">
            <MList Class="red lighten-3">
                <MListItem Title="@_model?.Name" Subtitle="API"></MListItem>
                <MListItem Title="@_model?.Auth" Subtitle="Auth"></MListItem>
                <MListItem Title="@_model?.Category" Subtitle="Category"></MListItem>
                <MListItem Title="@_model?.Cors" Subtitle="Cors"></MListItem>
                <MListItem Title="@_model?.Description" Subtitle="Description"></MListItem>
                <MListItem Title="@_model?.Https.ToString()" Subtitle="Https"></MListItem>
            </MList>
        </ShowTransitionElement>
    </ExpandTransition>
    <MCardActions>
        <MSpacer></MSpacer>
        <MButton Disabled="_model == null"
                 Color="grey darken-3"
                 OnClick="Clear">
            Clear
            <MIcon Right>
                mdi-close-circle
            </MIcon>
        </MButton>
    </MCardActions>
</MCard>

@code {

    private record Item(string Name, string Description, string Auth, bool Https, string Cors, string Category);

    [Inject] public IHttpClientFactory HttpClientFactory { get; set; } = null!;

    private List<Item> _entries = new();
    private bool _isLoading;
    private Item? _model;
    private string? _value;
    private bool Show => !string.IsNullOrEmpty(_value);
    private HttpClient? _httpClient;

    public async Task UpdateSearchInputAsync(string val)
    {
        _httpClient ??= HttpClientFactory.CreateClient("masa-docs");

        if (_entries.Count > 0)
        {
            return;
        }

        if (_isLoading)
        {
            return;
        }

        _isLoading = true;

        await Task.Delay(500); // Simulate a delay
        var res = await _httpClient.GetFromJsonAsync<List<Item>>("_content/Masa.Blazor.Docs/data/demo/public-apis.json");
        _entries = res ?? [];

        _isLoading = false;
    }

    public void Clear()
    {
        _model = null;
        _value = null;
    }

}